﻿
cmake_minimum_required(VERSION 3.12...3.15 FATAL_ERROR)
set(project_name parser)
project(${project_name})

set(LIB_NAME parser)
set(LIB_VERSION 1.0)
set(_INSTALL_INCLUDE_DIR "include/${LIB_NAME}-${LIB_VERSION}")
set(_INSTALL_CONFIG_DIR "lib/cmake/${LIB_NAME}-${LIB_VERSION}")
set(_LIBRARY_OUTPUT_PATH  "${PROJECT_BINARY_DIR}/lib")
set(_EXECUTABLE_OUTPUT_PATH "${PROJECT_BINARY_DIR}/bin")


set(CMAKE_CXX_STANDARD 17)

# 第三方库
add_subdirectory(contrib) 

set(headers
    AnyParser.h
    CommandLine.h
    Configuration.h
    Factory.h
    getpot.h
    GetpotParser.h
    Logging.h
    Mesh.h
    Object.h
    Parser.h
    Function.h
)

set(src
    Configuration.cpp
    Function.cpp
)

# ----------------------------  定义库 --------------------------------
add_library(${LIB_NAME} ${src})

target_include_directories(${LIB_NAME} INTERFACE
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/>
  $<INSTALL_INTERFACE:${_INSTALL_INCLUDE_DIR}>
)
install(FILES ${headers}
        DESTINATION ${_INSTALL_INCLUDE_DIR}
)

target_link_libraries(${LIB_NAME} PUBLIC fmt::fmt)

set_property(TARGET ${LIB_NAME} PROPERTY ARCHIVE_OUTPUT_DIRECTORY ${_LIBRARY_OUTPUT_PATH})
set_property(TARGET ${LIB_NAME} PROPERTY LIBRARY_OUTPUT_DIRECTORY ${_LIBRARY_OUTPUT_PATH})
set_property(TARGET ${LIB_NAME} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${_EXECUTABLE_OUTPUT_PATH})

# ------------------------------------------------------------------

include(CTest)
enable_testing()

set(_this common_test)
add_executable(${_this} Testing.cpp)

set_property(TARGET ${_this} PROPERTY ARCHIVE_OUTPUT_DIRECTORY ${_LIBRARY_OUTPUT_PATH})
set_property(TARGET ${_this} PROPERTY LIBRARY_OUTPUT_DIRECTORY ${_LIBRARY_OUTPUT_PATH})
set_property(TARGET ${_this} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${_EXECUTABLE_OUTPUT_PATH})

target_link_libraries(${_this}
PRIVATE gtest_main
PRIVATE ${LIB_NAME}
)

add_test(
    NAME ${_this}
    COMMAND ${_this}
)






# 1. 生成、安装xxConfigVersion.cmake
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
        ${CMAKE_CURRENT_BINARY_DIR}/${_INSTALL_CONFIG_DIR}/ParserConfigVersion.cmake
        VERSION ${LIB_VERSION}
        COMPATIBILITY ExactVersion  
        )

install(
        FILES ${CMAKE_CURRENT_BINARY_DIR}/${_INSTALL_CONFIG_DIR}/ParserConfigVersion.cmake
        DESTINATION ${_INSTALL_CONFIG_DIR}
)

set(_EXPORT_NAME ParserTargets)

# 2. 安装库文件xxx.lib
install(TARGETS ${LIB_NAME}
        EXPORT ${_EXPORT_NAME}
)

# export目的是在lib/下生成xxTargets,为了方便查看，可以忽略，install(EXPORT ...)也可以生成
export(EXPORT ${_EXPORT_NAME}
        NAMESPACE parser::
        FILE ${CMAKE_CURRENT_BINARY_DIR}/${_INSTALL_CONFIG_DIR}/ParserTargets.cmake
)

# 3. 生成安装xxTargets.cmake和xxTargets-[build type].cmake
install(EXPORT ${_EXPORT_NAME}
        NAMESPACE parser::
        DESTINATION ${_INSTALL_CONFIG_DIR}
        FILE ParserTargets.cmake
)

# 4. 安装xxConfig.cmake
#configure_file(
#               ParserConfig.cmake.in 
#               ${CMAKE_CURRENT_BINARY_DIR}/${_INSTALL_CONFIG_DIR}/ParserConfig.cmake @ONLY
#)
#

configure_package_config_file(
  ParserConfig.cmake.in 
  ${CMAKE_CURRENT_BINARY_DIR}/${_INSTALL_CONFIG_DIR}/ParserConfig.cmake
  INSTALL_DESTINATION ${_INSTALL_CONFIG_DIR}
)

install(
        FILES ${PROJECT_BINARY_DIR}/${_INSTALL_CONFIG_DIR}/ParserConfig.cmake
        DESTINATION ${_INSTALL_CONFIG_DIR}
)

# 4. 可执行文件
install(TARGETS common_test)